package mymac.com.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.text.TextUtilsCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Toast;

/**
 * Created by mymac on 2017/4/10.
 */

public class TestActivity extends AppCompatActivity {
    private NotificationManager manager;
    private Notification notis;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("is download ?");
        builder.setCancelable(true);
        builder.setMessage(" '天空之城-李志' ");
        builder.setNegativeButton("Download", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(TestActivity.this, "Download...", Toast.LENGTH_SHORT).show();
                manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notis = new Notification();

                Intent intent = new Intent();
                PendingIntent pendingIntent = PendingIntent.getActivity(TestActivity.this,0,intent,0);
                notis.defaults = Notification.DEFAULT_SOUND;
                notis.flags = Notification.FLAG_NO_CLEAR;
                notis.tickerText = "下载提示";
                notis.contentIntent = pendingIntent;
                notis.icon = R.mipmap.ic_launcher_round;
                notis.when = System.currentTimeMillis();
                notis.contentView = new RemoteViews(getPackageName(),R.layout.noti);
                notis.contentView.setTextViewText(R.id.tvMusicName,"天空之城");
                notis.contentView.setTextViewText(R.id.tvProgress,"0kb");
                notis.contentView.setTextViewText(R.id.tvTotalLength,"100kb");

                notis.contentView.setProgressBar(R.id.pbDownload,100,0,false);

                manager.notify(R.id.pbDownload,notis);

                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        for (int i=0; i<=100; i+=10){
                            try {
                                sleep(800);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            notis.contentView.setProgressBar(R.id.pbDownload,100,i,false);
                            notis.contentView.setTextViewText(R.id.tvProgress, i + "kb");
                            manager.notify(R.id.pbDownload,notis);
                        }
                        manager.cancel(R.id.pbDownload);
                    }
                }.start();
                startActivity();
            }
        });
        builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(TestActivity.this, "You select Cancel", Toast.LENGTH_SHORT).show();
                startActivity();
            }
        });
        builder.show();
        setContentView(R.layout.activity_test);
    }

    private void startActivity(){
        startActivity(new Intent(TestActivity.this,MainActivity.class));
        finish();
    }
}
